package com.eco.netty;

import com.eco.websocket.WebSocketServer;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/netty/NettyClient.class */
public class NettyClient {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) NettyClient.class);

    @Value("${sensor.tcpserver2.ip}")
    private String sensor2Host;

    @Value("${sensor.tcpserver2.port}")
    private int sensor2Port;

    @Value("${sensor.tcpserver3.ip}")
    private String sensor3Host;

    @Value("${sensor.tcpserver3.port}")
    private int sensor3Port;

    @Value("${sensor.tcpserver6.ip}")
    private String sensor6Host;

    @Value("${sensor.tcpserver6.port}")
    private int sensor6Port;

    @Value("${knd.tcp-server-jk.ip}")
    private String jkKndHost;

    @Value("${knd.tcp-server-jk.port}")
    private int jkKndPort;

    @Value("${knd.tcp-server-ck.ip}")
    private String ckKndHost;

    @Value("${knd.tcp-server-ck.port}")
    private int ckKndPort;
    private ChannelFuture future;
    private NioEventLoopGroup sensor4EventLoopGroup;
    private NioEventLoopGroup sensor5EventLoopGroup;
    private Bootstrap sensor2Bootstrap;
    private Bootstrap sensor3Bootstrap;
    private Bootstrap sensor6Bootstrap;
    private Bootstrap sensor4Bootstrap;
    private Bootstrap sensor5Bootstrap;
    private int num = 0;
    private boolean flag = true;
    private final NioEventLoopGroup sensor2EventLoopGroup = new NioEventLoopGroup();
    private final NioEventLoopGroup sensor3EventLoopGroup = new NioEventLoopGroup();
    private final NioEventLoopGroup sensor6EventLoopGroup = new NioEventLoopGroup();

    public void setSensor2Host(final String sensor2Host) {
        this.sensor2Host = sensor2Host;
    }

    public void setSensor2Port(final int sensor2Port) {
        this.sensor2Port = sensor2Port;
    }

    public void setSensor3Host(final String sensor3Host) {
        this.sensor3Host = sensor3Host;
    }

    public void setSensor3Port(final int sensor3Port) {
        this.sensor3Port = sensor3Port;
    }

    public void setSensor6Host(final String sensor6Host) {
        this.sensor6Host = sensor6Host;
    }

    public void setSensor6Port(final int sensor6Port) {
        this.sensor6Port = sensor6Port;
    }

    public void setJkKndHost(final String jkKndHost) {
        this.jkKndHost = jkKndHost;
    }

    public void setJkKndPort(final int jkKndPort) {
        this.jkKndPort = jkKndPort;
    }

    public void setCkKndHost(final String ckKndHost) {
        this.ckKndHost = ckKndHost;
    }

    public void setCkKndPort(final int ckKndPort) {
        this.ckKndPort = ckKndPort;
    }

    public void setNum(final int num) {
        this.num = num;
    }

    public void setFlag(final boolean flag) {
        this.flag = flag;
    }

    public void setFuture(final ChannelFuture future) {
        this.future = future;
    }

    public void setSensor4EventLoopGroup(final NioEventLoopGroup sensor4EventLoopGroup) {
        this.sensor4EventLoopGroup = sensor4EventLoopGroup;
    }

    public void setSensor5EventLoopGroup(final NioEventLoopGroup sensor5EventLoopGroup) {
        this.sensor5EventLoopGroup = sensor5EventLoopGroup;
    }

    public void setSensor2Bootstrap(final Bootstrap sensor2Bootstrap) {
        this.sensor2Bootstrap = sensor2Bootstrap;
    }

    public void setSensor3Bootstrap(final Bootstrap sensor3Bootstrap) {
        this.sensor3Bootstrap = sensor3Bootstrap;
    }

    public void setSensor6Bootstrap(final Bootstrap sensor6Bootstrap) {
        this.sensor6Bootstrap = sensor6Bootstrap;
    }

    public void setSensor4Bootstrap(final Bootstrap sensor4Bootstrap) {
        this.sensor4Bootstrap = sensor4Bootstrap;
    }

    public void setSensor5Bootstrap(final Bootstrap sensor5Bootstrap) {
        this.sensor5Bootstrap = sensor5Bootstrap;
    }

    public String getSensor2Host() {
        return this.sensor2Host;
    }

    public int getSensor2Port() {
        return this.sensor2Port;
    }

    public String getSensor3Host() {
        return this.sensor3Host;
    }

    public int getSensor3Port() {
        return this.sensor3Port;
    }

    public String getSensor6Host() {
        return this.sensor6Host;
    }

    public int getSensor6Port() {
        return this.sensor6Port;
    }

    public String getJkKndHost() {
        return this.jkKndHost;
    }

    public int getJkKndPort() {
        return this.jkKndPort;
    }

    public String getCkKndHost() {
        return this.ckKndHost;
    }

    public int getCkKndPort() {
        return this.ckKndPort;
    }

    public int getNum() {
        return this.num;
    }

    public boolean isFlag() {
        return this.flag;
    }

    public ChannelFuture getFuture() {
        return this.future;
    }

    public NioEventLoopGroup getSensor2EventLoopGroup() {
        return this.sensor2EventLoopGroup;
    }

    public NioEventLoopGroup getSensor3EventLoopGroup() {
        return this.sensor3EventLoopGroup;
    }

    public NioEventLoopGroup getSensor6EventLoopGroup() {
        return this.sensor6EventLoopGroup;
    }

    public NioEventLoopGroup getSensor4EventLoopGroup() {
        return this.sensor4EventLoopGroup;
    }

    public NioEventLoopGroup getSensor5EventLoopGroup() {
        return this.sensor5EventLoopGroup;
    }

    public Bootstrap getSensor2Bootstrap() {
        return this.sensor2Bootstrap;
    }

    public Bootstrap getSensor3Bootstrap() {
        return this.sensor3Bootstrap;
    }

    public Bootstrap getSensor6Bootstrap() {
        return this.sensor6Bootstrap;
    }

    public Bootstrap getSensor4Bootstrap() {
        return this.sensor4Bootstrap;
    }

    public Bootstrap getSensor5Bootstrap() {
        return this.sensor5Bootstrap;
    }

    @PostConstruct
    public void runSensor2() throws UnknownHostException {
        this.sensor2Bootstrap = new Bootstrap();
        this.sensor2Bootstrap.group(this.sensor2EventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() { // from class: com.eco.netty.NettyClient.1
            @Override // io.netty.channel.ChannelInitializer
            public void initChannel(SocketChannel ch) throws Exception {
                NettyClient.log.info("正在连接进口流速...");
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new Sensor2Encoder());
                pipeline.addLast(new FixedLengthFrameDecoder(25));
                pipeline.addLast(new Sensor2Decoder());
                pipeline.addLast(new IdleStateHandler(30L, 0L, 0L, TimeUnit.MINUTES));
                pipeline.addLast(new Sensor2HexHandler());
            }
        });
        sensor2Reconnect(this.sensor2Host, Integer.valueOf(this.sensor2Port));
    }

    @PostConstruct
    public void runSensor3() throws UnknownHostException {
        this.sensor3Bootstrap = new Bootstrap();
        this.sensor3Bootstrap.group(this.sensor3EventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() { // from class: com.eco.netty.NettyClient.2
            @Override // io.netty.channel.ChannelInitializer
            public void initChannel(SocketChannel ch) throws Exception {
                NettyClient.log.info("正在连接出口流速...");
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new Sensor3Encoder());
                pipeline.addLast(new FixedLengthFrameDecoder(25));
                pipeline.addLast(new Sensor3Decoder());
                pipeline.addLast(new IdleStateHandler(30L, 0L, 0L, TimeUnit.MINUTES));
                pipeline.addLast(new Sensor3HexHandler());
            }
        });
        sensor3Reconnect(this.sensor3Host, Integer.valueOf(this.sensor3Port));
    }

    public void runSensor6() throws UnknownHostException {
        this.sensor6Bootstrap = new Bootstrap();
        this.sensor6Bootstrap.group(this.sensor6EventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() { // from class: com.eco.netty.NettyClient.3
            @Override // io.netty.channel.ChannelInitializer
            public void initChannel(SocketChannel ch) throws Exception {
                NettyClient.log.info("正在连接涵洞水位计...");
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new Sensor6Encoder());
                pipeline.addLast(new FixedLengthFrameDecoder(13));
                pipeline.addLast(new Sensor6Decoder());
                pipeline.addLast(new IdleStateHandler(30L, 0L, 0L, TimeUnit.MINUTES));
                pipeline.addLast(new Sensor6HexHandler());
            }
        });
    }

    /* JADX WARN: Type inference failed for: r1v13, types: [io.netty.channel.ChannelFuture] */
    public boolean runKndJK() {
        this.sensor4Bootstrap = new Bootstrap();
        this.sensor4EventLoopGroup = new NioEventLoopGroup();
        this.sensor4Bootstrap.group(this.sensor4EventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() { // from class: com.eco.netty.NettyClient.4
            @Override // io.netty.channel.ChannelInitializer
            public void initChannel(SocketChannel ch) throws Exception {
                NettyClient.log.info("正在连接knd-jk...");
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new KndEncoderJK());
                pipeline.addLast(new FixedLengthFrameDecoder(12));
                pipeline.addLast(new KndDecoderJK());
                pipeline.addLast(new IdleStateHandler(5L, 0L, 0L, TimeUnit.MINUTES));
                pipeline.addLast(new KndHexHandlerJK());
            }
        });
        try {
            this.future = this.sensor4Bootstrap.connect(this.jkKndHost, this.jkKndPort).sync();
            this.future.addListener((GenericFutureListener<? extends Future<? super Void>>) new ChannelFutureListener() { // from class: com.eco.netty.NettyClient.5
                @Override // io.netty.util.concurrent.GenericFutureListener
                public void operationComplete(ChannelFuture arg0) throws Exception {
                    if (NettyClient.this.future.isSuccess()) {
                        NettyClient.log.info("连接knd-jk服务器成功");
                        return;
                    }
                    NettyClient.log.info("连接knd-jk服务器失败");
                    NettyClient.this.flag = false;
                    NettyClient.this.sensor4EventLoopGroup.shutdownGracefully();
                }
            });
        } catch (Exception e) {
            log.error("连接knd-jk异常:{}", (Throwable) e);
            this.flag = false;
            this.sensor4EventLoopGroup.shutdownGracefully();
        }
        return this.flag;
    }

    /* JADX WARN: Type inference failed for: r1v13, types: [io.netty.channel.ChannelFuture] */
    public boolean runKndCK() {
        this.sensor5Bootstrap = new Bootstrap();
        this.sensor5EventLoopGroup = new NioEventLoopGroup();
        this.sensor5Bootstrap.group(this.sensor5EventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() { // from class: com.eco.netty.NettyClient.6
            @Override // io.netty.channel.ChannelInitializer
            public void initChannel(SocketChannel ch) throws Exception {
                NettyClient.log.info("正在连接knd-ck...");
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new KndEncoderCK());
                pipeline.addLast(new FixedLengthFrameDecoder(12));
                pipeline.addLast(new KndDecoderCK());
                pipeline.addLast(new IdleStateHandler(5L, 0L, 0L, TimeUnit.MINUTES));
                pipeline.addLast(new KndHexHandlerCK());
            }
        });
        try {
            this.future = this.sensor5Bootstrap.connect(this.ckKndHost, this.ckKndPort).sync();
            this.future.addListener((GenericFutureListener<? extends Future<? super Void>>) new ChannelFutureListener() { // from class: com.eco.netty.NettyClient.7
                @Override // io.netty.util.concurrent.GenericFutureListener
                public void operationComplete(ChannelFuture arg0) throws Exception {
                    if (NettyClient.this.future.isSuccess()) {
                        NettyClient.log.info("连接knd-ck服务器成功");
                        return;
                    }
                    NettyClient.log.info("连接knd-ck服务器失败");
                    NettyClient.this.flag = false;
                    NettyClient.this.sensor5EventLoopGroup.shutdownGracefully();
                }
            });
        } catch (Exception e) {
            log.error("连接knd-ck异常:{}", (Throwable) e);
            this.flag = false;
            this.sensor5EventLoopGroup.shutdownGracefully();
        }
        return this.flag;
    }

    public void sensor2Reconnect(String host, Integer port) {
        this.sensor2Bootstrap.remoteAddress(host, port.intValue());
        ChannelFuture channelFuture = this.sensor2Bootstrap.connect();
        channelFuture.addListener((GenericFutureListener<? extends Future<? super Void>>) future -> {
            if (future.cause() != null) {
                log.error(host + ":" + port + " connect failure,retry...");
                WebSocketServer.sendInfo("jkls", "bl");
                ((ChannelFuture)future).channel().eventLoop().schedule(() -> {
                    sensor2Reconnect(host, port);
                }, 20L, TimeUnit.SECONDS);
                return;
            }
            log.info(host + ":" + port + " connect success!");
        });
    }

    public void sensor3Reconnect(String host, Integer port) {
        this.sensor3Bootstrap.remoteAddress(host, port.intValue());
        ChannelFuture channelFuture = this.sensor3Bootstrap.connect();
        channelFuture.addListener((GenericFutureListener<? extends Future<? super Void>>) future -> {
            if (future.cause() != null) {
                log.error(host + ":" + port + " connect failure,retry...");
                WebSocketServer.sendInfo("ckls", "bl");
                ((ChannelFuture)future).channel().eventLoop().schedule(() -> {
                    sensor3Reconnect(host, port);
                }, 23L, TimeUnit.SECONDS);
                return;
            }
            log.info(host + ":" + port + " connect success!");
        });
    }

    public void sensor6Reconnect(String host, Integer port) {
        this.sensor6Bootstrap.remoteAddress(host, port.intValue());
        ChannelFuture channelFuture = this.sensor6Bootstrap.connect();
        channelFuture.addListener((GenericFutureListener<? extends Future<? super Void>>) future -> {
            if (future.cause() != null) {
                log.error(host + ":" + port + " 连接涵洞水位计串口服务失败，重试...");
                ((ChannelFuture)future).channel().eventLoop().schedule(() -> {
                    sensor6Reconnect(host, port);
                }, 26L, TimeUnit.SECONDS);
            } else {
                log.info(host + ":" + port + " 连接涵洞水位计串口服务成功!");
            }
        });
    }

    @PreDestroy
    public void shutdown() {
        this.sensor2EventLoopGroup.shutdownGracefully();
        this.sensor3EventLoopGroup.shutdownGracefully();
        log.info("断开流速仪串口服务器连接");
    }

    @PreDestroy
    public void shutdownKndJK() {
        this.sensor4EventLoopGroup.shutdownGracefully();
        log.info("断开进口康耐德继电器连接");
    }

    @PreDestroy
    public void shutdownKndCK() {
        this.sensor5EventLoopGroup.shutdownGracefully();
        log.info("断开出口康耐德继电器连接");
    }
}